package ex.com.jm.food.action;





import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Company;
import ex.com.jm.food.entity.CompanyType;
import ex.com.jm.food.service.CompanyService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.MD5Utils;

/**
 * 企业的控制层
 * 
 * @author zhaojw
 *
 */
public class CompanyAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Company company;// 企业
	private String vcode;// 接收验证码信息
	private CompanyService companyService;// 业务层
	private String companyName;
	private String companyId;
	private String comEmail;
	private String comPwd;
	private String province;
	private String citys;
	private String countys;
	private String streets;
	private String errorInfo;// 提供错误信息
    private int comtypeId;
	private String bankCard;
	private String faceImage;
	private String backImage;
    private List<CompanyType> comType=new ArrayList<CompanyType>();
   

	public List<CompanyType> getComType() {
		return comType;
	}

	public void setComType(List<CompanyType> comType) {
		this.comType = comType;
	}

	public String getBankCard() {
		return bankCard;
	}

	public void setBankCard(String bankCard) {
		this.bankCard = bankCard;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCitys() {
		return citys;
	}

	public void setCitys(String citys) {
		this.citys = citys;
	}

	public String getCountys() {
		return countys;
	}

	public void setCountys(String countys) {
		this.countys = countys;
	}

	public String getStreets() {
		return streets;
	}

	public void setStreets(String streets) {
		this.streets = streets;
	}

	public String getErrorInfo() {
		return errorInfo;
	}

	public void setErrorInfo(String errorInfo) {
		this.errorInfo = errorInfo;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public String getVcode() {
		return vcode;
	}

	public void setVcode(String vcode) {
		this.vcode = vcode;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public String getComEmail() {
		return comEmail;
	}

	public void setComEmail(String comEmail) {
		this.comEmail = comEmail;
	}

	public String getComPwd() {
		return comPwd;
	}

	public void setComPwd(String comPwd) {
		this.comPwd = comPwd;
	}

	public int getComtypeId() {
		return comtypeId;
	}

	public void setComtypeId(int comtypeId) {
		this.comtypeId = comtypeId;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getFaceImage() {
		return faceImage;
	}

	public void setFaceImage(String faceImage) {
		this.faceImage = faceImage;
	}

	public String getBackImage() {
		return backImage;
	}

	public void setBackImage(String backImage) {
		this.backImage = backImage;
	}

	/**
	 * 到达登录页面
	 * 
	 * @return
	 */
	public String login(){
		return "comLogin";
	}
	
	public String Index(){
		return "comIndex";
	}

	
	
	public String edit() {
	comType=this.getCompanyService().selectComType();
	
		if (companyId != null) { 
			System.out.println(companyId);
			// href="register?comId="
			company = this.getCompanyService().selectCompany(companyId);
		}

		return "edit";

	}

	public String editCom() {
		comType=this.getCompanyService().selectComType();
			if (companyId != null) { 
				company = this.getCompanyService().selectCompany(companyId);
			}
			return "editCom";
		}
	
	
	/**
	 * 企业登录
	 * 
	 * @return
	 */
	public String companyLogin() {

		String serverCode = (String) ActionContext.getContext().getSession()
				.get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if (vcode.equalsIgnoreCase(serverCode)) {
			String password = "";
			try {
				// 对密码加密后在去查询
				password = MD5Utils.GetMD5Code(comPwd);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Company companyLogin = companyService.comLogin(comEmail, password);
			System.out.println(companyLogin);
			if (companyLogin != null) {
				// 登录成功,将企业信息放入session中
				ActionContext.getContext().getSession()
						.put("company", companyLogin);
				return "success";
			} else {
				addFieldError("errorInfo", "邮箱或密码错误，请重新输入");
				return "fail";
			}
		} else {
			addFieldError("errorInfo", "验证码输入错误");
			return "fail";
		}
	}

	/**
	 * 添加/修改企业
	 * 
	 * @return
	 */
	public String companySave() {
		System.out.println(company.getComName());
		
		if (company.getComName() == null || company.getComName().equals("")) {
			addFieldError("errorInfo", "企业名称不能为空");
			return "savefial";
		} else if (company.getComEmail() == null
				|| company.getComEmail().equals("")) {
			addFieldError("errorInfo", "企业邮箱不能为空");
			return "savefial";
		} else if (company.getComPwd() == null
				|| company.getComPwd().equals("")) {
			addFieldError("errorInfo", "企业登录密码不能为空");
			return "savefial";
		} else if (company.getComPwd().length() > 10
				|| company.getComPwd().length() < 5) {
			addFieldError("errorInfo", "企业登录密码为6到9位");
			return "savefial";
		}  
		if (company.getBankCard() == null
				|| company.getBankCard().equals("")
				|| company.getBankCard().length() != 16|| company.getBankCard().length() != 18|| company.getBankCard().length() != 19) {
		   
			// 验证邮箱
			Company checkEmail = this.getCompanyService().checkEmail(
					company.getComEmail());
		
			// 验证企业姓名
			Company checkName = this.getCompanyService().checkName(
					company.getComName());
		

			Company checkBankCard = this.getCompanyService().checkBankCard(
					company.getBankCard());
			if (checkEmail != null && company.getComId() == null) {
				addFieldError("errorInfo", "注册邮箱已存在");
				return "savefial";
			}
			if (checkName != null && company.getComId() == null) {
				addFieldError("errorInfo", "企业名称已存在");
				return "savefial";
			}
			if (company.getComId() == null || company.getComId().equals("")) {
				company.setStatus(0);
				company.setCreated(DateUtils.getCurrentDate());
			}
			if (checkBankCard != null && company.getComId() == null) {// 后面的更新用
				addFieldError("errorInfo", "银行卡已经被使用");
				return "savefial";
			}
			
			String password = "";
			try {
				// 密码加密
				password = MD5Utils.GetMD5Code(company.getComPwd());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.err.println(password);
			company.setComPwd(password);
			
			String comAddres=province+";"+citys+";"+countys+";"+streets;
			if (comAddres==";;;") {
				addFieldError("province", "地址不能为空");
				return "savefial";
			}
			company.setComAddress(comAddres);
			company.setComtypeId(this.getCompanyService().getComType(comtypeId));
			if (company.getComId() != null && !company.getComId().equals("")) {
				System.out.println("1111" + company.toString());
				boolean f = this.getCompanyService().updateCompany(company);
				if (f) {
					// 更新成功，跳转到列表页
					return "companyList";
				} else {
					return "savefial";
				}

			} else {

				companyService.saveCompany(company);
				return "login";
			}
		}else {
			addFieldError("company.bankCard", "银行卡必须是十六位十八位或者十九位");
			return "savefial";
		}
			
		

		
	}
	
	
	public String editIDCard(){
		if(companyId!=null){
			String cardImage=this.getCompanyService().selectCompany(companyId).getIdCard();
			if(cardImage!=null){
			faceImage=cardImage.split(";")[0];
			backImage=cardImage.split(";")[1];
			}
		}
		return "editIDCard";
	}
	
	public String updateIDCard(){
		if(faceImage==null||faceImage.equals("")){
			addFieldError("faceImage", "请上传正面照片");
			return "editIDCard";
		}
		if(backImage==null||backImage.equals("")){
			addFieldError("backImage", "请上传反面照片");
			return "editIDCard";
		}
		String cardImage=faceImage+";"+backImage;
		this.getCompanyService().updateIDCard(companyId, cardImage);
		return "login";
	}
	
	public String LoginOut(){
		ActionContext ac = ActionContext.getContext();
		Map session = ac.getSession();
		session.remove("company");
		return "Index";
	}
	
}
